<cfcomponent displayname="Utility" output="false" initmethod="init" access="public">
	<cfset variables.requireRequestBody = "post,put">
	<cfset variables.statusCode = "201,204">
	<cffunction name="init" access="public" output="false">
		<cfreturn this>
	</cffunction>
	
	<cffunction name="debug" access="public" output="false" hint="file write">
		<cfargument name="contents" required="true">
		<cffile action="append" file="C:\ColdFusion9\wwwroot\CTCT_Wrapper\debug.txt" output="#arguments.contents#">
	</cffunction>
	
	<cffunction name="httpDebug" access="public" output="false" hint="http status code debugging" returntype="boolean">
		<cfargument name="httpResponse" required="true">
		<cfscript>
			local.statusCode = arguments.httpResponse.responseHeader['status_code'];
			if(listContains(variables.statusCode, local.statusCode)) {
				return true;
			} else {
				return false;
			}
		
		</cfscript>
	</cffunction>
	
	<cffunction name="CTCTRequest" access="public" output="false" hint="Processes CTCT WebRequests">
		<cfargument name="method" required="true" type="string" hint="valid options: get, put, post, delete">
		<cfargument name="address" required="true" type="string">
		<cfargument name="requestValue" type="any">
		<cfscript>
		local.httpRequest = new Http(username=application.apiKey & '%' & application.ccUsername, password=application.ccPassword);
		local.httpRequest.setMethod('#arguments.method#');
		local.httpRequest.setUrl('#arguments.address#');
		local.httpRequest.addParam(type='header', name='Content-Type', value='application/atom+xml');
		local.httpRequest.addParam(type='header', name='accept', value='application/atom+xml');
		if(listContains(variables.requireRequestBody, arguments.method)) {
			local.httpRequest.addParam(type='body', value='#arguments.requestValue#'); 
		}
			local.httpResponse = local.httpRequest.send().getPrefix();
			debug(local.httpResponse);
			debug("--------------------");
		return local.httpResponse.fileContent;
		</cfscript>	
	</cffunction>
	
	<cffunction name="multiPartPost" access="public" output="false" returntype="any">
		<cfargument name="activityType" required="true" type="string">
		<cfargument name="lists" required="true" type="array">
		<cfargument name="dataFile" type="string">
		<cfscript>
			local.httpRequest = new Http(username=application.apiKey & '%' & application.ccUsername, password=application.ccPassword);
			local.httpRequest.setMethod('post');
			local.httpRequest.setUrl(application.apiPath & '/activities');
			local.httpRequest.addParam(type='header', name='accept', value='application/atom+xml');
			local.httpRequest.addParam(type='formField', name='activityType', value='#arguments.activityType#');
			for (i=1; i LTE arrayLen(arguments.lists); i=i+1) {
				local.httpRequest.addParam(type='formField', name='lists', value='#arguments.lists[i]#');
			}
			if(isdefined('arguments.dataFile')) {
				local.httpRequest.addParam(type='formField', name='data', value='#arguments.dataFile#');
			}
			local.httpResponse = local.httpRequest.send().getPrefix();
			return local.httpResponse.fileContent;
		</cfscript>
</cffunction>


	<cffunction name="urlencodedPost" access="public" output="false" returntype="any">
		<cfargument name="activityType" required="true" type="string">
		<cfargument name="lists" required="true" type="array">
		<cfargument name="fileContents" type="string">
		
		<cfscript>
		local.httpRequest = new Http(username=application.apiKey & '%' & application.ccUsername, password=application.ccPassword);
		local.httpRequest.setUrl(application.apiPath & '/activities');
		local.httpRequest.setMethod('post');
		//local.httpRequest.addParam(type='header', name='Content-Type', value='multipart/form-data');
		local.httpRequest.addParam(type='header', name='accept', value='application/atom+xml');
		local.httpRequest.addParam(type='formField', name='activityType', value='#arguments.activityType#');
		for (i=1; i LTE arrayLen(arguments.lists); i=i+1) {
			local.httpRequest.addParam(type='formField', name='lists', value='#arguments.lists[i]#');
		}
		if(isdefined('arguments.dataFile')) {
			local.httpRequest.addParam(type='formField', name='data', value='#arguments.fileContents#', encoded='true');
		}
		local.httpResponse = local.httpRequest.send().getPrefix();
		return local.httpResponse.fileContent;
		</cfscript>
	</cffunction>
</cfcomponent>
